package com.kws.merchant.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kws.merchant.domain.Order;
import com.kws.merchant.domain.vo.ConfirmGoodsVo;
import com.kws.merchant.domain.vo.ContractOrderVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author : zhangbenting
 * @date : 2024/11/5 15:03
 * @Version: 1.0
 */
public interface UserOrderMapper extends BaseMapper<Order> {

    @Select("<script>" +
            "SELECT " +
            " a.id AS goods_id, " +
            " a.`name` as goods_name, " +
            " a.price, " +
            " a.img, " +
            " b.id AS merchant_id, " +
            " b.company_name AS merchant_name, " +
            " c.id AS specification_id, " +
            " c.color, " +
            " c.specification_model, " +
            " c.img AS specification_img  " +
            "FROM " +
            " tb_device AS a " +
            " LEFT JOIN tb_merchant AS b ON a.merchant_id = b.id " +
            " LEFT JOIN tb_specification AS c ON a.id = c.goods_id  " +
            "WHERE " +
            " a.id = ${goodsId}  " +
            " <if test='specificationId!=null and specificationId!=\"\"'>  AND c.id = ${specificationId}  </if>  " +
            "</script>")
    ConfirmGoodsVo getConfirmOrderInfo(@Param("goodsId") Long goodsId,
                                       @Param("specificationId") Long specificationId);


    @Select("SELECT " +
            " a.id AS goods_id, " +
            " a.`name` AS goods_name, " +
            " a.price, " +
            " a.img, " +
            " b.id AS merchant_id, " +
            " b.company_name AS merchant_name  " +
            "FROM " +
            " tb_device AS a " +
            " LEFT JOIN tb_merchant AS b ON a.merchant_id = b.id  " +
            "WHERE " +
            " a.id in (${goodsIds})")
    List<ConfirmGoodsVo> getDeviceInfoList(@Param("goodsIds") String goodsIds);

    @Update("UPDATE tb_order SET status = #{status} WHERE id = #{orderId}")
    int updateOrderStatus(@Param("orderId") Long orderId, @Param("status") int status);

    @Select("<script>" +
            "SELECT   " +
            "   a.order_no,   " +
            "   a.merchant_name,   " +
            "   a.create_time,   " +
            "   b.sign_status,   " +
            "   c.item_scheme_price    " +
            "FROM   " +
            "   tb_order AS a   " +
            "   LEFT JOIN tb_item AS b ON a.item_id = b.id   " +
            "   LEFT JOIN tb_item_scheme AS c ON b.id = c.item_id    " +
            "WHERE   " +
            "   a.`status` = 0    " +
            "   AND a.type = 1    " +
            "   AND a.user_id = ${userId} " +
            " <if test='id!=null and id!=\"\"'>  AND a.id = ${id} </if>  " +
            "</script>")
    List<ContractOrderVo> getContractUserOrder(@Param("userId") Long userId,
                                               @Param("id") Long id);

    @Select("<script>" +
            "SELECT   " +
            "  count(a.order_no)   " +
            "FROM   " +
            "   tb_order AS a   " +
            "   LEFT JOIN tb_item AS b ON a.item_id = b.id   " +
            "   LEFT JOIN tb_item_scheme AS c ON b.id = c.item_id    " +
            "WHERE   " +
            "   a.`status` = 0    " +
            "   AND a.type = 1    " +
            "   AND a.user_id = ${userId} " +
            " <if test='id!=null and id!=\"\"'>  AND a.id = ${id}   </if>  " +
            "</script>")
    Long getContractUserOrderCount(@Param("userId") Long userId,
                                   @Param("id") Long id);
}
